package com.college.express.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.college.express.R;

/**
 * @Description: JAVA类作用描述
 * @Author: LYJ
 * @CreateDate: 2021/4/14 14:13
 * @UpdateUser: LYJ
 * @UpdateDate: 2021/4/14 14:13
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
  protected final String TAG = this.getClass().getSimpleName();
  protected BaseActivity mContext;

  public static final int TITLE_TYPE_GREEN = 1;
  public static final int TITLE_TYPE_WHITE = 2;

  protected TextView mTvTitle;
  protected TextView mTvTitleRight;
  protected ImageView mIvTitleLeft;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setStatusShow();
  }

  @Override
  public void setContentView(int layoutResID) {
    if (showTitle()) {
      //如果需要显示自定义toolbar,并且资源id存在的情况下，实例化baseView;
      //根布局
      View mRootView = LayoutInflater.from(this).inflate(R.layout.layout_root, null, false);
      //toolbar容器
      ViewStub mVsTitle = (ViewStub) mRootView.findViewById(R.id.vs_title);
      //子布局容器
      FrameLayout mFlContainer = (FrameLayout) mRootView.findViewById(R.id.fl_container);
      //toolbar资源id
      mVsTitle.setLayoutResource(R.layout.layout_title_white);
      //填充toolbar
      mVsTitle.inflate();
      //子布局
      LayoutInflater.from(this).inflate(layoutResID, mFlContainer, true);
      setContentView(mRootView);

      mTvTitle = (TextView) findViewById(R.id.tv_title_center);
      mTvTitleRight = (TextView) findViewById(R.id.tv_title_right);
      mIvTitleLeft = (ImageView) findViewById(R.id.iv_title_left);

      mTvTitle.setText(TextUtils.isEmpty(setTitle()) ? "" : setTitle());

      if (showBack()) {
        mIvTitleLeft.setOnClickListener(this);
        mIvTitleLeft.setOnClickListener(v -> finish());
      } else {
        mIvTitleLeft.setVisibility(View.GONE);
      }
    } else {
      //不显示通用toolbar
      super.setContentView(layoutResID);
    }
  }

  /**
   * 是否显示标题栏
   */
  protected abstract boolean showTitle();

  /**
   * 设置标题
   */
  protected abstract String setTitle();


  /**
   * 初始化UI
   */
  protected abstract void initView();

  protected boolean showBack() {
    return true;
  }


  /**
   * 初始化数据
   */
  protected abstract void initData();


  /**
   * 修改状态栏显示的颜色,状态栏改为白色
   */
  protected void setStatusShow() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      this.getWindow().setStatusBarColor(Color.WHITE);
      this.getWindow().getDecorView().setSystemUiVisibility(
          View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
          WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
  }
}
